package cn.daixi.web.sys.service.impl;

import cn.daixi.web.sys.dto.TableColumnRes;
import cn.daixi.web.sys.dto.TableRes;
import cn.daixi.web.sys.mapper.UserMapper;
import cn.daixi.web.sys.service.SysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

@Service
public class SysServiceImpl implements SysService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DataSource dataSource;

    public String getDbName() throws SQLException {
        return dataSource.getConnection().getCatalog();
    }

    public List<TableColumnRes> getTableColumns(String tableName) {
        String dbName = null;
        try {
            dbName = getDbName();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return userMapper.getTableColumns(dbName, tableName);
    }

    public List<TableRes> getTables() {
        String dbName = null;
        try {
            dbName = getDbName();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return userMapper.getTables(dbName);
    }

}
